home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / ztype.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  12.0 KB  |  524 lines

  1. /* Copyright (C) 1989, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: ztype.c,v 1.3 2000/09/19 19:00:55 lpd Exp $ */
  20. /* Type, attribute, and conversion operators */
  21. #include "math_.h"
  22. #include "memory_.h"
  23. #include "string_.h"
  24. #include "gsexit.h"
  25. #include "ghost.h"
  26. #include "oper.h"
  27. #include "imemory.h"        /* for register_ref_root */
  28. #include "idict.h"
  29. #include "iname.h"
  30. #include "stream.h"        /* for iscan.h */
  31. #include "strimpl.h"        /* for sfilter.h for picky compilers */
  32. #include "sfilter.h"        /* ditto */
  33. #include "iscan.h"
  34. #include "iutil.h"
  35. #include "dstack.h"        /* for dict_set_top */
  36. #include "store.h"
  37.  
  38. /*
  39.  * Some of the procedures in this file are public only so they can be
  40.  * called from the FunctionType 4 interpreter (zfunc4.c).
  41.  */
  42.  
  43. /* Forward references */
  44. private int access_check(P3(i_ctx_t *, int, bool));
  45. private int convert_to_string(P2(os_ptr, os_ptr));
  46.  
  47. /*
  48.  * Max and min integer values expressed as reals.
  49.  * Note that these are biased by 1 to allow for truncation.
  50.  * They should be #defines rather than static consts, but
  51.  * several of the SCO Unix compilers apparently can't handle this.
  52.  * On the other hand, the DEC compiler can't handle casts in
  53.  * constant expressions, so we can't use min_long and max_long.
  54.  * What a nuisance!
  55.  */
  56. #define ALT_MIN_LONG (-1L << (arch_sizeof_long * 8 - 1))
  57. #define ALT_MAX_LONG (~(ALT_MIN_LONG))
  58. private const double min_int_real = (ALT_MIN_LONG * 1.0 - 1);
  59. private const double max_int_real = (ALT_MAX_LONG * 1.0 + 1);
  60.  
  61. #define REAL_CAN_BE_INT(v)\
  62.   ((v) > min_int_real && (v) < max_int_real)
  63.  
  64. /* Get the pointer to the access flags for a ref. */
  65. #define ACCESS_REF(opp)\
  66.   (r_has_type(opp, t_dictionary) ? dict_access_ref(opp) : opp)
  67.  
  68. /* <obj> <typenames> .type <name> */
  69. private int
  70. ztype(i_ctx_t *i_ctx_p)
  71. {
  72.     os_ptr op = osp;
  73.     ref tnref;
  74.     int code = array_get(op, (long)r_btype(op - 1), &tnref);
  75.  
  76.     if (code < 0)
  77.     return code;
  78.     if (!r_has_type(&tnref, t_name)) {
  79.     /* Must be either a stack underflow or a t_[a]struct. */
  80.     check_op(2);
  81.     {            /* Get the type name from the structure. */
  82.         const char *sname =
  83.         gs_struct_type_name_string(gs_object_type(imemory,
  84.                               op[-1].value.pstruct));
  85.         int code = name_ref((const byte *)sname, strlen(sname),
  86.                 (ref *) (op - 1), 0);
  87.  
  88.         if (code < 0)
  89.         return code;
  90.     }
  91.     r_set_attrs(op - 1, a_executable);
  92.     } else {
  93.     ref_assign(op - 1, &tnref);
  94.     }
  95.     pop(1);
  96.     return 0;
  97. }
  98.  
  99. /* - .typenames <name1|null> ... <nameN|null> */
  100. private int
  101. ztypenames(i_ctx_t *i_ctx_p)
  102. {
  103.     os_ptr op = osp;
  104.     static const char *const tnames[] = { REF_TYPE_NAME_STRINGS };
  105.     int i;
  106.  
  107.     check_ostack(t_next_index);
  108.     for (i = 0; i < t_next_index; i++) {
  109.     ref *const rtnp = op + 1 + i;
  110.  
  111.     if (i >= countof(tnames) || tnames[i] == 0)
  112.         make_null(rtnp);
  113.     else {
  114.         int code = name_enter_string(tnames[i], rtnp);
  115.  
  116.         if (code < 0)
  117.         return code;
  118.         r_set_attrs(rtnp, a_executable);
  119.     }
  120.     }
  121.     osp += t_next_index;
  122.     return 0;
  123. }
  124.  
  125. /* <obj> cvlit <obj> */
  126. private int
  127. zcvlit(i_ctx_t *i_ctx_p)
  128. {
  129.     os_ptr op = osp;
  130.     ref *aop;
  131.  
  132.     check_op(1);
  133.     aop = ACCESS_REF(op);
  134.     r_clear_attrs(aop, a_executable);
  135.     return 0;
  136. }
  137.  
  138. /* <obj> cvx <obj> */
  139. int
  140. zcvx(i_ctx_t *i_ctx_p)
  141. {
  142.     os_ptr op = osp;
  143.     ref *aop;
  144.     uint opidx;
  145.  
  146.     check_op(1);
  147.     /*
  148.      * If the object is an internal operator, we can't allow it to
  149.      * exist in executable form anywhere outside the e-stack.
  150.      */
  151.     if (r_has_type(op, t_operator) &&
  152.     ((opidx = op_index(op)) == 0 ||
  153.      op_def_is_internal(op_index_def(opidx)))
  154.     )
  155.     return_error(e_rangecheck);
  156.     aop = ACCESS_REF(op);
  157.     r_set_attrs(aop, a_executable);
  158.     return 0;
  159. }
  160.  
  161. /* <obj> xcheck <bool> */
  162. private int
  163. zxcheck(i_ctx_t *i_ctx_p)
  164. {
  165.     os_ptr op = osp;
  166.  
  167.     check_op(1);
  168.     make_bool(op, (r_has_attr(ACCESS_REF(op), a_executable) ? 1 : 0));
  169.     return 0;
  170. }
  171.  
  172. /* <obj:array|packedarray|file|string> executeonly <obj> */
  173. private int
  174. zexecuteonly(i_ctx_t *i_ctx_p)
  175. {
  176.     os_ptr op = osp;
  177.  
  178.     check_op(1);
  179.     if (r_has_type(op, t_dictionary))
  180.     return_error(e_typecheck);
  181.     return access_check(i_ctx_p, a_execute, true);
  182. }
  183.  
  184. /* <obj:array|packedarray|dict|file|string> noaccess <obj> */
  185. private int
  186. znoaccess(i_ctx_t *i_ctx_p)
  187. {
  188.     os_ptr op = osp;
  189.  
  190.     check_op(1);
  191.     if (r_has_type(op, t_dictionary)) {
  192.     /*
  193.      * Setting noaccess on a read-only dictionary is an attempt to
  194.      * change its value, which is forbidden (this is a subtle
  195.      * point confirmed with Adobe).  Also, don't allow removing
  196.      * read access to permanent dictionaries.
  197.      */
  198.     if (dict_is_permanent_on_dstack(op) ||
  199.         !r_has_attr(dict_access_ref(op), a_write)
  200.         )
  201.         return_error(e_invalidaccess);
  202.     }
  203.     return access_check(i_ctx_p, 0, true);
  204. }
  205.  
  206. /* <obj:array|packedarray|dict|file|string> readonly <obj> */
  207. int
  208. zreadonly(i_ctx_t *i_ctx_p)
  209. {
  210.     return access_check(i_ctx_p, a_readonly, true);
  211. }
  212.  
  213. /* <array|packedarray|dict|file|string> rcheck <bool> */
  214. private int
  215. zrcheck(i_ctx_t *i_ctx_p)
  216. {
  217.     os_ptr op = osp;
  218.     int code = access_check(i_ctx_p, a_read, false);
  219.  
  220.     if (code >= 0)
  221.     make_bool(op, code), code = 0;
  222.     return code;
  223. }
  224.  
  225. /* <array|packedarray|dict|file|string> wcheck <bool> */
  226. private int
  227. zwcheck(i_ctx_t *i_ctx_p)
  228. {
  229.     os_ptr op = osp;
  230.     int code = access_check(i_ctx_p, a_write, false);
  231.  
  232.     if (code >= 0)
  233.     make_bool(op, code), code = 0;
  234.     return code;
  235. }
  236.  
  237. /* <num> cvi <int> */
  238. /* <string> cvi <int> */
  239. int
  240. zcvi(i_ctx_t *i_ctx_p)
  241. {
  242.     os_ptr op = osp;
  243.     float fval;
  244.  
  245.     switch (r_type(op)) {
  246.     case t_integer:
  247.         return 0;
  248.     case t_real:
  249.         fval = op->value.realval;
  250.         break;
  251.     default:
  252.         return_op_typecheck(op);
  253.     case t_string:
  254.         {
  255.         ref str, token;
  256.         int code;
  257.  
  258.         ref_assign(&str, op);
  259.         code = scan_string_token(i_ctx_p, &str, &token);
  260.         if (code > 0)    /* anything other than a plain token */
  261.             code = gs_note_error(e_syntaxerror);
  262.         if (code < 0)
  263.             return code;
  264.         switch (r_type(&token)) {
  265.             case t_integer:
  266.             *op = token;
  267.             return 0;
  268.             case t_real:
  269.             fval = token.value.realval;
  270.             break;
  271.             default:
  272.             return_error(e_typecheck);
  273.         }
  274.         }
  275.     }
  276.     if (!REAL_CAN_BE_INT(fval))
  277.     return_error(e_rangecheck);
  278.     make_int(op, (long)fval);    /* truncates towards 0 */
  279.     return 0;
  280. }
  281.  
  282. /* <string> cvn <name> */
  283. private int
  284. zcvn(i_ctx_t *i_ctx_p)
  285. {
  286.     os_ptr op = osp;
  287.  
  288.     check_read_type(*op, t_string);
  289.     return name_from_string(op, op);
  290. }
  291.  
  292. /* <num> cvr <real> */
  293. /* <string> cvr <real> */
  294. int
  295. zcvr(i_ctx_t *i_ctx_p)
  296. {
  297.     os_ptr op = osp;
  298.  
  299.     switch (r_type(op)) {
  300.     case t_integer:
  301.         make_real(op, op->value.intval);
  302.     case t_real:
  303.         return 0;
  304.     default:
  305.         return_op_typecheck(op);
  306.     case t_string:
  307.         {
  308.         ref str, token;
  309.         int code;
  310.  
  311.         ref_assign(&str, op);
  312.         code = scan_string_token(i_ctx_p, &str, &token);
  313.         if (code > 0)    /* anything other than a plain token */
  314.             code = gs_note_error(e_syntaxerror);
  315.         if (code < 0)
  316.             return code;
  317.         switch (r_type(&token)) {
  318.             case t_integer:
  319.             make_real(op, token.value.intval);
  320.             return 0;
  321.             case t_real:
  322.             *op = token;
  323.             return 0;
  324.             default:
  325.             return_error(e_typecheck);
  326.         }
  327.         }
  328.     }
  329. }
  330.  
  331. /* <num> <radix_int> <string> cvrs <substring> */
  332. private int
  333. zcvrs(i_ctx_t *i_ctx_p)
  334. {
  335.     os_ptr op = osp;
  336.     int radix;
  337.  
  338.     check_type(op[-1], t_integer);
  339.     if (op[-1].value.intval < 2 || op[-1].value.intval > 36)
  340.     return_error(e_rangecheck);
  341.     radix = op[-1].value.intval;
  342.     check_write_type(*op, t_string);
  343.     if (radix == 10) {
  344.     switch (r_type(op - 2)) {
  345.         case t_integer:
  346.         case t_real:
  347.         {
  348.             int code = convert_to_string(op - 2, op);
  349.  
  350.             if (code < 0)
  351.             return code;
  352.             pop(2);
  353.             return 0;
  354.         }
  355.         default:
  356.         return_op_typecheck(op - 2);
  357.     }
  358.     } else {
  359.     ulong ival;
  360.     byte digits[sizeof(ulong) * 8];
  361.     byte *endp = &digits[countof(digits)];
  362.     byte *dp = endp;
  363.  
  364.     switch (r_type(op - 2)) {
  365.         case t_integer:
  366.         ival = (ulong) op[-2].value.intval;
  367.         break;
  368.         case t_real:
  369.         {
  370.             float fval = op[-2].value.realval;
  371.  
  372.             if (!REAL_CAN_BE_INT(fval))
  373.             return_error(e_rangecheck);
  374.             ival = (ulong) (long)fval;
  375.         } break;
  376.         default:
  377.         return_op_typecheck(op - 2);
  378.     }
  379.     do {
  380.         int dit = ival % radix;
  381.  
  382.         *--dp = dit + (dit < 10 ? '0' : ('A' - 10));
  383.         ival /= radix;
  384.     }
  385.     while (ival);
  386.     if (endp - dp > r_size(op))
  387.         return_error(e_rangecheck);
  388.     memcpy(op->value.bytes, dp, (uint) (endp - dp));
  389.     r_set_size(op, endp - dp);
  390.     }
  391.     op[-2] = *op;
  392.     pop(2);
  393.     return 0;
  394. }
  395.  
  396. /* <any> <string> cvs <substring> */
  397. private int
  398. zcvs(i_ctx_t *i_ctx_p)
  399. {
  400.     os_ptr op = osp;
  401.     int code;
  402.  
  403.     check_op(2);
  404.     check_write_type(*op, t_string);
  405.     code = convert_to_string(op - 1, op);
  406.     if (code >= 0)
  407.     pop(1);
  408.     return code;
  409. }
  410.  
  411. /* ------ Initialization procedure ------ */
  412.  
  413. const op_def ztype_op_defs[] =
  414. {
  415.     {"1cvi", zcvi},
  416.     {"1cvlit", zcvlit},
  417.     {"1cvn", zcvn},
  418.     {"1cvr", zcvr},
  419.     {"3cvrs", zcvrs},
  420.     {"2cvs", zcvs},
  421.     {"1cvx", zcvx},
  422.     {"1executeonly", zexecuteonly},
  423.     {"1noaccess", znoaccess},
  424.     {"1rcheck", zrcheck},
  425.     {"1readonly", zreadonly},
  426.     {"2.type", ztype},
  427.     {"0.typenames", ztypenames},
  428.     {"1wcheck", zwcheck},
  429.     {"1xcheck", zxcheck},
  430.     op_def_end(0)
  431. };
  432.  
  433. /* ------ Internal routines ------ */
  434.  
  435. /* Test or modify the access of an object. */
  436. /* If modify = true, restrict to the selected access and return 0; */
  437. /* if modify = false, do not change the access, and return 1 */
  438. /* if the object had the access. */
  439. /* Return an error code if the object is not of appropriate type, */
  440. /* or if the object did not have the access already when modify=1. */
  441. private int
  442. access_check(i_ctx_t *i_ctx_p,
  443.          int access,    /* mask for attrs */
  444.          bool modify)    /* if true, reduce access */
  445. {
  446.     os_ptr op = osp;
  447.     ref *aop;
  448.  
  449.     switch (r_type(op)) {
  450.     case t_dictionary:
  451.         aop = dict_access_ref(op);
  452.         if (modify) {
  453.         if (!r_has_attrs(aop, access))
  454.             return_error(e_invalidaccess);
  455.         ref_save(op, aop, "access_check(modify)");
  456.         r_clear_attrs(aop, a_all);
  457.         r_set_attrs(aop, access);
  458.         dict_set_top();
  459.         return 0;
  460.         }
  461.         break;
  462.     case t_array:
  463.     case t_file:
  464.     case t_string:
  465.     case t_mixedarray:
  466.     case t_shortarray:
  467.     case t_astruct:
  468.     case t_device:;
  469.         if (modify) {
  470.         if (!r_has_attrs(op, access))
  471.             return_error(e_invalidaccess);
  472.         r_clear_attrs(op, a_all);
  473.         r_set_attrs(op, access);
  474.         return 0;
  475.         }
  476.         aop = op;
  477.         break;
  478.     default:
  479.         return_op_typecheck(op);
  480.     }
  481.     return (r_has_attrs(aop, access) ? 1 : 0);
  482. }
  483.  
  484. /* Do all the work of cvs.  The destination has been checked, but not */
  485. /* the source.  This is a separate procedure so that */
  486. /* cvrs can use it when the radix is 10. */
  487. private int
  488. convert_to_string(os_ptr op1, os_ptr op)
  489. {
  490.     uint len;
  491.     const byte *pstr = 0;
  492.     int code = obj_cvs(op1, op->value.bytes, r_size(op), &len, &pstr);
  493.  
  494.     if (code < 0) {
  495.     /*
  496.      * Some common downloaded error handlers assume that
  497.      * operator names don't exceed a certain fixed size.
  498.      * To work around this bit of bad design, we implement
  499.      * a special hack here: if we got a rangecheck, and
  500.      * the object is an operator whose name begins with
  501.      * %, ., or @, we just truncate the name.
  502.      */
  503.     if (code == e_rangecheck)
  504.         switch (r_btype(op1)) {
  505.         case t_oparray:
  506.         case t_operator:
  507.             if (pstr != 0)
  508.             switch (*pstr) {
  509.                 case '%':
  510.                 case '.':
  511.                 case '@':
  512.                 len = r_size(op);
  513.                 memcpy(op->value.bytes, pstr, len);
  514.                 goto ok;
  515.             }
  516.         }
  517.     return code;
  518.     }
  519. ok:
  520.     *op1 = *op;
  521.     r_set_size(op1, len);
  522.     return 0;
  523. }
  524.